package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.operate.trading.api.parameter.DelayOrderAutoReceiveTimeBean;
import com.ymatou.operate.trading.api.service.DelayOrderAutoReceiveTimeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 延长收货时间 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_DelayOrderAutoReceiveTime {

	private static DelayOrderAutoReceiveTimeBean doartbean;
	private static DelayOrderAutoReceiveTimeCall doartcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("延长收货时间");
	}

	@Before
	public void caseUp() {

		doartbean = new DelayOrderAutoReceiveTimeBean();
		doartcall = new DelayOrderAutoReceiveTimeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(doartcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DelayOrderAutoReceiveTime_001() {
		Logger.start(true,"卖家延长收货时间成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额	
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);			
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);			
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			int delayDay = 1;
			doartbean.setDelayDay(delayDay);
			String delayTime = YMTDateUtil.getSysTime();
			doartbean.setDelayTime(delayTime);
			doartbean.setOrderId(orderId);
			doartbean.setSellerId(sellerId);
			// doartbean.setMemo("自动化测试延长信息");
			// 发送请求
			doartcall.setData(doartbean);
			doartcall.callService();
			Logger.verifyEquals(ECode.SUCCESS, doartcall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG, doartcall.getMsg(),
			// "验证Msg");
			// 数据库验证
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(1, database.selectOrderFrozenByOrderId(orderId).size(), "验证订单延长收货记录表");
			HashMap m = new HashMap();
			m.put("OrderId", orderId);
			Thread.sleep(3000);
			List<Map> tnlist = database.selectTradingNotifaction(m);
			// 确认接单 发货 付款
			//Logger.verifyEquals(3, tnlist.size(), "验证交易通知表");
			/*for (Map map2 : tnlist) {
				Logger.debug(map2.get("iOrderId"));
				Logger.debug(map2.get("sTradingOperate"));
			}*/
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayOrderAutoReceiveTime_002() {
		Logger.start(false,"此订单不存在");
		try {
			int orderId = 1234;
			int sellerId = 288484;
			int delayDay = 1;
			doartbean.setDelayDay(delayDay);
			String delayTime = YMTDateUtil.getSysTime();
			doartbean.setDelayTime(delayTime);
			doartbean.setOrderId(orderId);
			doartbean.setSellerId(sellerId);
			// doartbean.setMemo("自动化测试延长信息");
			// 发送请求
			doartcall.setData(doartbean);
			doartcall.callService();
			Logger.verifyEquals(ECode.FAILED201, doartcall.getCode(), "验证Code");
			Logger.verifyIsContains(ECode.FAILED201_MSG_11, doartcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayOrderAutoReceiveTime_003() {
		Logger.start(true,"当前订单还没有超过可再次延期的时间限制");
		try {
			// 创建订单
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);			
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);			
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			int delayDay = 1;
			doartbean.setDelayDay(delayDay);
			String delayTime = YMTDateUtil.getSysTime();
			doartbean.setDelayTime(delayTime);
			doartbean.setOrderId(orderId);
			doartbean.setSellerId(sellerId);
			// doartbean.setMemo("自动化测试延长信息");
			// 发送请求
			doartcall.setData(doartbean);
			doartcall.callService();
			doartcall.callService();
			Logger.verifyEquals(ECode.FAILED201, doartcall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.FAILED201_MSG_29, doartcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
    @Category(P3.class)
    @TestCase
    public void Tc_DelayOrderAutoReceiveTime_004() {
        Logger.start(false,"订单号为null");
        try {
            int orderId = 1234;
            int sellerId = 288484;
            int delayDay = 1;
            doartbean.setDelayDay(delayDay);
            String delayTime = YMTDateUtil.getSysTime();
            doartbean.setDelayTime(delayTime);
            //doartbean.setOrderId(orderId);
            doartbean.setSellerId(sellerId);
            doartbean.setRemark("自动化测试延长信息");
            // 发送请求
            doartcall.setData(doartbean);
            doartcall.callService();
            Logger.verifyEquals(ECode.ERROR400, doartcall.getCode(), "验证Code");
            Logger.verifyIsContains(ECode.ERROR400_MSG_9, doartcall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
	   @Test
	    @Category(P3.class)
	    @TestCase
	    public void Tc_DelayOrderAutoReceiveTime_005() {
	        Logger.start(false,"卖家id为null");
	        try {
	            int orderId = 1234;
	            int sellerId = 288484;
	            int delayDay = 1;
	            doartbean.setDelayDay(delayDay);
	            String delayTime = YMTDateUtil.getSysTime();
	            doartbean.setDelayTime(delayTime);
	            doartbean.setOrderId(orderId);
	            //doartbean.setSellerId(sellerId);
	            doartbean.setRemark("自动化测试延长信息");
	            // 发送请求
	            doartcall.setData(doartbean);
	            doartcall.callService();
	            Logger.verifyEquals(ECode.ERROR400, doartcall.getCode(), "验证Code");
	            Logger.verifyIsContains(ECode.ERROR400_MSG_35, doartcall.getMsg(), "验证Msg");
	        } catch (Exception e) {
	            Logger.fail(e);
	        }
	    }
	   @Test
       @Category(P3.class)
       @TestCase
       public void Tc_DelayOrderAutoReceiveTime_006() {
           Logger.start(false,"延长的时间为0");
           try {
               int orderId = 1234;
               int sellerId = 288484;
               int delayDay = 0;
               doartbean.setDelayDay(delayDay);
               String delayTime = YMTDateUtil.getSysTime();
               doartbean.setDelayTime(delayTime);
               doartbean.setOrderId(orderId);
               doartbean.setSellerId(sellerId);
               doartbean.setRemark("自动化测试延长信息");
               // 发送请求
               doartcall.setData(doartbean);
               doartcall.callService();
               Logger.verifyEquals(ECode.ERROR400, doartcall.getCode(), "验证Code");
               Logger.verifyIsContains(ECode.ERROR400_MSG_16, doartcall.getMsg(), "验证Msg");
           } catch (Exception e) {
               Logger.fail(e);
           }
       }
	   @Test
       @Category(P3.class)
       @TestCase
       public void Tc_DelayOrderAutoReceiveTime_007() {
           Logger.start(false,"延长的时间小于0");
           try {
               int orderId = 1234;
               int sellerId = 288484;
               int delayDay = -1;
               doartbean.setDelayDay(delayDay);
               String delayTime = YMTDateUtil.getSysTime();
               doartbean.setDelayTime(delayTime);
               doartbean.setOrderId(orderId);
               doartbean.setSellerId(sellerId);
               doartbean.setRemark("自动化测试延长信息");
               // 发送请求
               doartcall.setData(doartbean);
               doartcall.callService();
               Logger.verifyEquals(ECode.ERROR400, doartcall.getCode(), "验证Code");
               Logger.verifyIsContains(ECode.ERROR400_MSG_16, doartcall.getMsg(), "验证Msg");
           } catch (Exception e) {
               Logger.fail(e);
           }
       }
	    @Test
	    @Category(P3.class)
	    @TestCase
	    public void Tc_DelayOrderAutoReceiveTime_008() {
	        Logger.start(false,"当前订单状态不是已发货,延长收货失败");
	        try {
	            // 创建订单
	            Logger.comment("前置数据准备");       
	            PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

	            int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
	            int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
	            int userId =placeOrderResp.getUserId();         
	            int tradingId = placeOrderResp.getTradingId();

	            BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
	            // 支付订单
	            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);          
	            // 接单
	            new OrderCallService().acceptOrder(orderId, sellerId);          
/*	            // 商家发货
	            new OrderCallService().dispatched(orderId, sellerId);*/
	            Logger.comment("前置数据准备结束");
	            int delayDay = 1;
	            doartbean.setDelayDay(delayDay);
	            String delayTime = YMTDateUtil.getSysTime();
	            doartbean.setDelayTime(delayTime);
	            doartbean.setOrderId(orderId);
	            doartbean.setSellerId(sellerId);
	            doartbean.setRemark("自动化测试延长信息");
	            // 发送请求
	            doartcall.setData(doartbean);
	            doartcall.callService();
	            doartcall.callService();
	            Logger.verifyEquals(ECode.FAILED201, doartcall.getCode(), "验证Code");
	            Logger.verifyEquals(ECode.FAILED201_MSG_64, doartcall.getMsg(), "验证Msg");
	        } catch (Exception e) {
	            Logger.fail(e);
	        }
	    }
}